﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyMvc.BLL;
using MyMvc.IBLL;
using MyMvc.Model;
using MyMvc.Model.Models;

namespace BlueHome.Admin.Controllers
{
    public class UserInfoController : Controller
    {
        //
        // GET: /UserInfo/
        IUserService _userService = new UserService();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllUser()
        {
            //分页的数据
            //
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);

            //封装一个业务逻辑层，来处理分页过滤的事件
            GetModelQuery userInfoQuery = new GetModelQuery();
            userInfoQuery.pageIndex = pageIndex;
            userInfoQuery.pageSize = pageSize;
            userInfoQuery.total = 0;

            //放置依赖刷新
            var data = from u in _userService.LoadSearchData(userInfoQuery)
                       select new { u.ID, u.UserName, u.PassWord };

            //var data = _userInfoService.LoadPagerEntities(pageSize, pageIndex, out total, u => true, true, u => u.ID);

            var result = new { total = userInfoQuery.total, rows = data };

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="deleteUserInfoID"></param>
        /// <returns></returns>
        public ActionResult DeleteUserInfo(string deleteUserInfoID, string UName)
        {
            //首先确认是哪个管理员登录进来的，如果此用户正在登录系统，则不允许删除此用户
            User UInfo = Session["User"] as User;
            var LoginUName = UInfo.UserName;

            var UIdsName = UName.Split(',');
            List<string> deleteUName = new List<string>();
            foreach (var Name in UIdsName)
            {
                deleteUName.Add(Name);
            }
            if (deleteUName.Contains(LoginUName))
            {
                return Content("含有正在使用的用户，禁止删除");
            }

            if (string.IsNullOrEmpty(deleteUserInfoID))
            {
                return Content("请选择您要删除的数据");
            }
            //截取传递过来的字符串的数字信息
            var idsStr = deleteUserInfoID.Split(',');

            List<int> deleteIDList = new List<int>();

            foreach (var ID in idsStr)
            {
                deleteIDList.Add(Convert.ToInt32(ID));
            }
            if (_userService.DeleteUserInfo(deleteIDList) > 0)
            {
                return Content("OK");
            }
            return Content("删除失败，请您检查");

        }

        public ActionResult AddUser(User user)
        {
            _userService.AddEntities(user);
            return Content("OK");
        }

        /// <summary>
        /// 绑定用户数据
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult GetBindDetails(int ID)
        {
            var BindIDForUpdateTextBox = _userService.LoadEntities(u => u.ID == ID).FirstOrDefault();
            return Json(BindIDForUpdateTextBox, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public ActionResult UpdateInfo(User userInfo)
        {
            _userService.UpdateEntities(userInfo);
            return Content("OK");
        }

    }
}
